﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Annotations;
using JBeamLibrary.Events;
using JBeamLibrary.VehicleParts.Interfaces;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public abstract class VehicleElement : IVehicleElement
    {
        #region Fields

        public event ElementPropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        public abstract IVehicleElement FromJBeam(JToken token);

        public abstract JToken ToJBeam();

        #endregion

        #region Static methods

        public static T NewFromJBeam<T>(JToken token) where T : VehicleElement
        {
            return (T)((T)Activator.CreateInstance(typeof(T))).FromJBeam(token);
        }

        public static JToken ElementToJBeam(VehicleElement element)
        {
            return element.ToJBeam();
        }

        #endregion

        #region INotifyPropertyChanged

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(object oldValue, object newValue, [CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new ElementPropertyChangedEventArgs(this, propertyName, oldValue, newValue));
        }

        #endregion
    }
}
